package com.rjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rjy.entity.Query;
import com.rjy.entity.Teacher;
import com.rjy.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin(origins = "*")
@RestController
public class TeacherController {
    @Autowired
    private TeacherMapper teacherMapper;
    @CrossOrigin(origins = "*")
    @RequestMapping("/queryAllTeacherByPage")
    public Map<String,Object> queryAllTeacherByPage(Query query){
        Page<Teacher> pages = new Page<>(query.getPage(), query.getLimit());
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();  
        if(query.getId()!=null && !"".equals(query.getId())){
            queryWrapper.eq("id",query.getId());
        }
        if(query.getName()!=null && !"".equals(query.getName())){
            queryWrapper.like("name","%"+query.getName()+"%");
        }
        if(query.getSex()!=null && !"".equals(query.getSex())){
            queryWrapper.eq("sex",query.getSex());
        }
        if(query.getHiredate()!=null && !"".equals(query.getHiredate())){
            queryWrapper.eq("hiredate",query.getHiredate());
        }
        Long count = teacherMapper.selectCount(queryWrapper);
        teacherMapper.selectPage(pages, queryWrapper);
        List<Teacher> list = pages.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);
        return map;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping("/insertTeacher")
    public Integer insertTeacher(Teacher teacher){
        teacherMapper.insertTeacher(teacher);
        return 1;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping("/updateTeacher")
    public Integer updateTeacher(Teacher teacher){
       int i= teacherMapper.updateById(teacher);
        return i;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping("deleteTeacherByIds")
    public Integer deleteTeacherByIds(@RequestBody List<Integer> ids){
        int cout= teacherMapper.deleteBatchIds(ids);
        return cout;
    }

}
